package cn.wolfcode.web.controller;

import cn.wolfcode.query.JsonResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.domain.Department;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    @RequiresPermissions(value = {"department:list","部门列表"},logical = Logical.OR)
    //@RequiredPermission(name = "部门列表", expression = "department:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        //PageResult<Department> result = departmentService.query(qo);
        PageInfo<Department> result = departmentService.query(qo);
        //int i = 10 / 0;
        //将查询出来的数据存入到模型中去
        model.addAttribute("pageInfo", result);
        return "department/list";
    }

/*    @RequiredPermission(name = "部门编辑",expression = "department:input")
    @RequestMapping("/input")
    public String input(Model model, Long id) {
        //判断页面传过来的id是否为空
        if (id != null) {
            //不为空则为编辑操作
            Department department = departmentService.get(id);
            model.addAttribute("department", department);
        }
        return "department/input";
    }*/



    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
            //判断页面传过来的id是否为空
            Long id = department.getId();
            if (id != null) {
                //不为空说明是更新操作
                departmentService.update(department);
            } else {
                //否则为新增操作
                departmentService.save(department);
            }
            return new JsonResult();

    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        if (id != null) {
            departmentService.delete(id);
        }
        return new JsonResult();

    }
}

